<?php

class Customers extends Controller {
	
	function customers()
	{
		parent::Controller();
		$this->load->library('table');
		$this->load->library('formdate');
		
		$this->load->model('Customers_model');
	}

	function index()
	{
		// Displays the initial customers view
		
		$this->load->view('customers/main_view');
	}
	
	function show()		
	{
		// Calls the Customers_model get() function, which returns an array of the 
		// complete customers table. It then sends this array to the show_view page.
		// If the URI contains a customer_id, it instead sets the $customer_id variable,
		// which tells the model's get() function to only return the record with the matching customer_id.
		// It then calls the update_view.
		
		if ($this->uri->segment(3))
		{
			$this->Customers_model->customer_id = $this->uri->segment(3);
					
			$data['result'] = $this->Customers_model->get();
			
			// Split up the date into the month, day, and year
			// and add these new values to the $data['result'] array
			list($data['result']->year, $data['result']->month, $data['result']->day) = split("-",$data['result']->dob);

			// Load the lookup table values from the corresponding tables
			$this->load->model('Cities_model');
			// $data['cities'] = $this->Cities_model->get_Cities();
			$data['cities'] = $this->Customers_model->get_Cities();
					
			$data['genders'] = $this->Customers_model->get_Genders();
			$data['races'] = $this->Customers_model->get_Races();
			$data['states'] = $this->Customers_model->get_States();
			$data['eye_colors'] = $this->Customers_model->get_Eye_colors();
			$data['hair_colors'] = $this->Customers_model->get_Hair_colors();

			
			$this->load->view('customers/update_view',$data);
		}
		else
		{
			$data['result'] = $this->Customers_model->get();
			$this->load->view('customers/showAll_view',$data);	
		}	
	}
	
	function add()		
	{
		// Adds a new customer
		// query all the lookup tables and store their results in an array
		// send the results to the view, in order to populate the dropdown fields
		
		
		$formdate = new FormDate();
		
		$formdate->year['start'] = 1920;
		$formdate->year['end'] = 2008;
		$formdate->month['values'] = 'numbers'; 
		$data['formdate'] = $formdate;
				
		$this->load->model('Cities_model');
		$data['cities'] = $this->Cities_model->get_Cities();
				
		$data['genders'] = $this->Customers_model->get_Genders();
		$data['races'] = $this->Customers_model->get_Races();
		$data['states'] = $this->Customers_model->get_States();
		$data['eye_colors'] = $this->Customers_model->get_Eye_colors();
		$data['hair_colors'] = $this->Customers_model->get_Hair_colors();
		
		$this->load->view('customers/add_view',$data);
	}
	
	function save()
	{
		// Receives the add_customer_form result.
		// Takes the year,month,day fields from the form and concatenates them into the $dob field.
		// It then adds the $dob field to the _POST variable, and calls the model's save() function.
		// Finally, it displays all of the customers.
		
		$dob = $this->input->post('year') . $this->input->post('month') . $this->input->post('day');
		
		$_POST['dob'] = $dob;	
				
		$this->Customers_model->save($_POST);
		
		redirect('customers/show');
	}
	
	function delete()		// delete a customer
	{
		//call the Customers model delete_Customers function
		
		if ($this->uri->segment(3))
			$this->Customers_model->delete($this->uri->segment(3));
		
		redirect('customers/show');

	}

}
?>
